<?php

namespace app\checkbill\controller;

use app\common\controller\PCBase;
use think\facade\Db;
use app\checkbill\logic\CheckbillLogic;
use app\checkbill\logic\BillLogic;
use app\checkbill\logic\BillQcLogic;
use app\checkbill\model\Checkbill as CheckbillModel;
use Throwable;

class Checkbill extends PCBase
{
    /**
     * 模型
     * @var object
     * @phpstan-var CheckbillModel
     */
    protected object $model;

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new CheckbillModel();
    }

    /**
     * 查看test
     * @throws Throwable
     */
    public function index()
    {
        $params = $this->request->post();
        $checkbillLogic = new CheckbillLogic();
        $result = $checkbillLogic->getcheckbillsList($params);

        return $result;
    }

    /**
     * 添加
     * @throws Throwable
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['createtime'] = time();
            if(!empty($data['files'])){
                $data['files'] = json_encode($data['files'],true);
            }
            if ($this->modelValidate) {
                try {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = new $validate();
                    $validate->scene('edit')->check($data);
                } catch (Throwable $e) {
                    return json(DefaultResponse(201, false, $e->getMessage(), null));
                }
            }

            $checkbillLogic = new checkbillLogic();
            $result = $checkbillLogic->addcheckbill($data);
            return $result;
        }
    }

    /**
     * 编辑
     * @throws Throwable
     */
    public function edit($id = null)
    {
        $row = $this->model->find($id);
        if (!$row) {
            return json(DefaultResponse(201, false, '记录不能为空!', []));
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->modelValidate) {
                try {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = new $validate();
                    $validate->scene('edit')->check($data);
                } catch (Throwable $e) {
                    return json(DefaultResponse(201, false, $e->getMessage(), null));
                }
            }

            $checkbillLogic = new checkbillLogic();
            $result = $checkbillLogic->editcheckbill($row, $data);

            return json(DefaultResponse(201, false, '记录不能为空!', $result));
        }

        if(!empty($row['files'])){
            $row['files'] = json_decode($row['files'],true);
        }
        return json(DefaultResponse(200, true, '获取成功', ['row' => $row]));
    }

    /**
     * 删除
     * @param null $ids
     * @throws Throwable
     */
    public function del($ids = null)
    {

        $data = $this->request->post();
        if (!$data||!$ids){
            return  json(DefaultResponse(201, false, '参数错误', null));
        }

        $pk    = $this->model->getPk();
        $data  = $this->model->where($pk, 'in', $ids)->select();
        
        $count = 0;
        $this->model->startTrans();
        try {
            foreach ($data as $v) {
                if ($v->id != $this->getAdminId()) {
                    $count += $v->delete();
                    Db::name('checkbill')
                        ->where('id', $v['id'])
                        ->delete();
                }
            }
            $this->model->commit();
        } catch (Throwable $e) {
            $this->model->rollback();
            return   json(DefaultResponse(201, false, $e->getMessage(), null));
        }
        if ($count) {
            return   json(DefaultResponse(200, true, '操作成功', null));
        } else {
            return   json(DefaultResponse(201, false, '操作失败', null));
        }
    }


    /**
     * @desc  顺丰账单写入表中
     * @author xx
     * @date 2024-03-18
     */
    public function importSFConsumer(){
        $file   = $this->request->file('file');
        $tel    = $this->getAdminTel();
        $data['file'] = $file;
        $data['tel']  = $tel;
        (new BillLogic())->import_SF_detail($data);
        return $this->jsonMessage(200, true, "推送成功!", null);
    }


    /**
     * @desc  全程无忧写入表中
     * @author xx
     * @date 2024-04-23
     */
    public function importQCConsumer(){
        $file         = $this->request->file('file');
        $tel          = $this->getAdminTel();
        $data['file'] = $file;
        $data['tel']  = $tel;
        (new BillQcLogic())->import_qc_detail($data);
        return $this->jsonMessage(200, true, "推送成功!", null);
    }





    /**
     * @desc  全程无忧
     * @author xx
     * @date 2024-04-25
     */
    public function checkQcProduce(){
        (new BillQcLogic())->checkQcProduce();
        return $this->jsonMessage(200, true, "推送成功!", null);
    }

    /**
     * @desc  全程无忧消费
     * @author xx
     * @date 2024-04-25
     */
    public function checkQcConsumer(){
        $data = $this->request->post();
        $res = (new BillQcLogic())->checkQcComsumer($data);
        return $this->jsonMessage(200, true, "推送成功!", $res);
    }
















}
